package com.l.web.util;

import com.l.web.master.system.config.ConfigDAO;
import org.apache.log4j.Logger;
import org.l.qiao.web.KeyUpdate;
import org.l.qiao.web.form.Form;
import org.l.qiao.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

public abstract class ServiceSupportBak {

    protected static final String TYPE = "_type";
    protected static final String TYPE_SAVE = "save";

    public static final String SUCCESS = "success";
    public static final String NONE = "none";
    public static final String LIST = "list";
    public static final String ADD = "add";
    public static final String DETAIL = "detail";
    public static final String SAVE = "save";
    public static final String UPDATE = "update";
    public static final String DELETE = "delete";

    protected Form form;

    private static final Logger LOGGER = Logger.getLogger(ServiceSupportBak.class);
    @Autowired
    private ConfigDAO configDAO;

    public ServiceSupportBak() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        form = new Form(request);
    }

//    public Config getConfigure(String key) {
//        Cluster cluster = configDAO.getConfig(key);
//        Config config = new Config((Map) Global.config.clone());
//        ClusterUtil.extend((Map) config.clone(), cluster);
//        return config;
//    }
//
//    public User getLoginUser() {
//        return (User) context.getSession().get(Global.SESSION_USER);
//    }

    public Page getPage() {
        Page page = new Page();
        form.getBean(page, false, "page");
        return page;
    }

    public KeyUpdate getKeyUpdate(Class clazz) {
        KeyUpdate keyUpdate = new KeyUpdate(clazz);
        form.getBean(keyUpdate, false, "update");
        return keyUpdate;
    }

    public abstract String execute();

//    public String error(int code) {
//        try {
//            response.sendError(code);
//        } catch (IOException e) {
//            LOGGER.error("设置http状态错误(" + code + "):" + e);
//        }
//        return NONE;
//    }
//
//    public boolean out(int code) {
//        ResponseInfo responseInfo = new ResponseInfo(code);
//        return out(responseInfo);
//    }

//    public boolean out(Object content) {
//
//        if (writer == null) {
//            try {
//                writer = response.getWriter();
//            } catch (IOException e) {
//                LOGGER.error("获取页面输出句柄异常：" + e);
//                return false;
//            }
//        }
//
//        writer.write(String.valueOf(content));
//        return true;
//    }
}
